﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;

namespace Puman
{
    public class Encrypt
    {
        public static string Cal(string source, string key)
        {
            var re = "";
            MD5 md5 = MD5.Create();
            byte[] bs = md5.ComputeHash(Encoding.Default.GetBytes(source + key));
            re = BitConverter.ToString(bs).Replace("-", "");
            return re.ToLower();
        }

        public static string toUrl(Dictionary<string, string> dic, string key)
        {
            var rst = "";

            string unionStr = unionMethod(dic);
            string md5Code = Cal(unionStr, key);
            rst = unionStr + "&md5Code=" + md5Code;

            return rst;
        }

        public static bool equal(Dictionary<string, string> dic, string key, string md5Code)
        {
            bool rst = false;

            string unionStr = unionMethod(dic);
            string md5CodeCaled = Cal(unionStr, key);
           rst= md5Code == md5CodeCaled;
            return rst;
        }

        static string unionMethod(Dictionary<string, string> dic)
        {
            return string.Join("&", dic.Select(c => c.Key + "=" + c.Value));
        }
    }
}
